package com.skplanet.rade.domain;

import java.io.Serializable;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import com.google.gson.annotations.Expose;

@Entity
@Table(name = "tag")
public class Tag implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public Tag() {
		this.uCount = 0;
		this.sCount = 0;
	}

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Expose
	private Long tid;
	
	@Column(name = "keyword", unique=true )
	@Expose
	private String keyword;
	
	@Column(name = "ucount")
	@Expose
	private int uCount;
	
	@Column(name = "scount")
	@Expose
	private int sCount;
	
	@OneToMany(mappedBy = "tag", fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
	@OrderBy("utid ASC" )
	private List<UserTag> userTags;
	
	@OneToMany(mappedBy = "tag", fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
	@OrderBy("stid ASC" )
	@Expose
	private List<StoryTag> storyTags;

	public Long getTid() {
		return tid;
	}

	public void setTid(Long tid) {
		this.tid = tid;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public int getuCount() {
		return uCount;
	}

	public void setuCount(int uCount) {
		this.uCount = uCount;
	}

	public void adduCount() {
		this.uCount = this.uCount + 1;
	}
	
	public int getsCount() {
		return sCount;
	}

	public void setsCount(int sCount) {
		this.sCount = sCount;
	}
	
	public void addsCount() {
		this.sCount = this.sCount + 1;
	}
	
	public void subsCount() {
		this.sCount = this.sCount - 1;
	}

	public List<UserTag> getUserTags() {
		return userTags;
	}

	public void setUserTags(List<UserTag> userTags) {
		this.userTags = userTags;
	}
	
	public void addUserTag(UserTag userTag){
		if(userTags == null){
			userTags = new ArrayList<UserTag>();
		}
		userTags.add(userTag);
	}

	public List<StoryTag> getStoryTags() {
		return storyTags;
	}

	public void setStoryTags(List<StoryTag> storyTags) {
		this.storyTags = storyTags;
	}
	
	public void addStoryTag(StoryTag storyTag){
		if(storyTags == null){
			storyTags = new ArrayList<StoryTag>();
		}
		storyTags.add(storyTag);
	}
	
	//UserTag 대소 비교 함수 
	private static final Comparator<Tag> tagComparator = new Comparator<Tag>() {
		
		@Override
		public int compare(Tag object1, Tag object2) {
			return (object1.getuCount() - object2.getuCount());
		}
	};

	public static Comparator<Tag> getTagcomparator() {
		return tagComparator;
	}
}
